package com.ffapp.demo.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.ffapp.demo.basis.Constants;
import com.ffapp.demo.greendao.GreenDaoUtils;

import java.util.ArrayList;
import java.util.Calendar;

import my.FileUtils;
import my.LogUtil;
import my.TimeUtils;

public class UserBaseInfoUpdateCache extends BasisBean {

    private boolean needUpdateUnit = true;
    private boolean needUpdateStation = true;
    private boolean needUpdateBuilding = true;
    private boolean needUpdateTrain= true;
    private boolean needUpdateUser = true;
    private boolean needUpdateRisk = true;
    private boolean needUpdateWater = true;

    private long unitTime;
    private long stationTime;
    private long buildingTime;
    private long trainTime;
    private long userTime;
    private long riskTime;
    private long waterTime;

    private int unitCount;
    private int waterCount;
    private int stationCount;
    private int userCount;
    private int buildingCount;
    private int riskCount;
    private int trainCount;

    private int unitCountUpdated;
    private int waterCountUpdated;
    private int stationCountUpdated;
    private int userCountUpdated;
    private int buildingCountUpdated;
    private int riskCountUpdated;
    private int trainCountUpdated;

    private boolean isForceUpdate = false;


    @JSONField(serialize = false)
    public boolean isNeedUpdate(){
        if(isForceUpdate )return true;
        LogUtil.i("-----time111---"+ TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));
        LogUtil.i("ContactListBean.isCacheExist()---?"+ContactListBean.isCacheExist());
        LogUtil.i(" userCount > 0---?"+( userCount > 0));

//        if(needUpdateUser ){
//            if(!ContactListBean.isCacheExist()){
//                needUpdateUser = true;
//                userTime = 0;
//            }else {
//                needUpdateUser = (userCount >0);
//            }
//        }

        if( needUpdateUnit ){
            if(GreenDaoUtils.getListBeanCountByType(GreenDaoUtils.TYPE_UNIT) == 0){
                needUpdateUnit = true;
                unitTime = 0;
            }else {
            }
            needUpdateUnit = (unitCount > 0);

        }

//        LogUtil.i("GreenDaoUtils.getListBeanCountByType(GreenDaoUtils.TYPE_station)---->"+GreenDaoUtils.getListBeanCountByType(GreenDaoUtils.TYPE_STATION));
        if(needUpdateStation ){
           if(GreenDaoUtils.getListBeanCountByType(GreenDaoUtils.TYPE_STATION) == 0 ){
               needUpdateStation = true;
               stationTime = 0;
           }else {
           }
            needUpdateStation =   (stationCount > 0);

        }

        if(needUpdateBuilding ){
            if(GreenDaoUtils.getListBeanCountByType(GreenDaoUtils.TYPE_BUILDING) == 0 ){
                needUpdateBuilding = true;
                buildingTime = 0;
            }else {
            }
            needUpdateBuilding = (  buildingCount > 0);

        }

        if(needUpdateTrain){
            if(trainTime > 0){
                Calendar calendarNow = Calendar.getInstance();
                Calendar calendarCache = Calendar.getInstance();
                calendarCache.setTimeInMillis(trainTime);
                if(calendarNow.get(Calendar.YEAR) != calendarCache.get(Calendar.YEAR)){
                    trainTime = 0;
                    GreenDaoUtils.deleteByMapType(GreenDaoUtils.TYPE_TRAIN);
                }
            }
            if(GreenDaoUtils.getListBeanCountByType(GreenDaoUtils.TYPE_TRAIN) == 0){
                needUpdateTrain = true;
                trainTime = 0;
            }else {
            }
            needUpdateTrain = ( trainCount > 0);
        }


        if(needUpdateRisk ){
            if(GreenDaoUtils.getListBeanCountByType(GreenDaoUtils.TYPE_RISK) == 0){
                needUpdateRisk = true;
                riskTime = 0;
            }else {
            }
            needUpdateRisk = (riskCount > 0);
        }

        if(needUpdateWater ){
            if(GreenDaoUtils.getListBeanCountByType(GreenDaoUtils.TYPE_WATER) == 0){
                needUpdateWater = false;
                waterTime = 0;
            }else {
            }
            needUpdateWater = ( waterCount > 0);
        }
//        listBean = GreenDaoUtils.getListBeanByType("unit");


        LogUtil.i("-----time222---"+ TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));
        if( needUpdateRisk ||needUpdateTrain || needUpdateBuilding ||needUpdateUnit||needUpdateWater||needUpdateStation)return true;

            return false;
    }


    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "ff_xmxf_baseinfo_update_cache_.data";

    @JSONField(serialize = false)
    public static UserBaseInfoUpdateCache getFromCache() {
        UserBaseInfoUpdateCache data = (UserBaseInfoUpdateCache) FileUtils.readObject(FILE_CACHE);
        if (data == null) {
//            data = new ContactListBean();
        }
        return data;
    }

    @JSONField(serialize = false)
    public void save() {
        isForceUpdate =false;
        FileUtils.cacheObject(FILE_CACHE, this);
    }
    @JSONField(serialize = false)
    public static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
    }




    public int getUnitCount() {
        return unitCount;
    }

    public void setUnitCount(int unitCount) {
        this.unitCount = unitCount;
    }


    public int getUserCount() {
        return userCount;
    }

    public void setUserCount(int userCount) {
        this.userCount = userCount;
    }

    public long getUnitTime() {
        return unitTime;
    }

    public void setUnitTime(long unitTime) {
        this.unitTime = unitTime;
    }

    public long getStationTime() {
        return stationTime;
    }

    public void setStationTime(long stationTime) {
        this.stationTime = stationTime;
    }

    public long getBuildingTime() {
        return buildingTime;
    }

    public void setBuildingTime(long buildingTime) {
        this.buildingTime = buildingTime;
    }

    public long getTrainTime() {
        return trainTime;
    }

    public void setTrainTime(long trainTime) {
        this.trainTime = trainTime;
    }

    public long getUserTime() {
        return userTime;
    }

    public void setUserTime(long userTime) {
        this.userTime = userTime;
    }

    public long getRiskTime() {
        return riskTime;
    }

    public void setRiskTime(long riskTime) {
        this.riskTime = riskTime;
    }

    public long getWaterTime() {
        return waterTime;
    }

    public void setWaterTime(long waterTime) {
        this.waterTime = waterTime;
    }

    public boolean isNeedUpdateUnit() {
        return needUpdateUnit;
    }

    public void setNeedUpdateUnit(boolean needUpdateUnit) {
        this.needUpdateUnit = needUpdateUnit;
    }

    public boolean isNeedUpdateStation() {
        return needUpdateStation;
    }

    public void setNeedUpdateStation(boolean needUpdateStation) {
        this.needUpdateStation = needUpdateStation;
    }

    public boolean isNeedUpdateBuilding() {
        return needUpdateBuilding;
    }

    public void setNeedUpdateBuilding(boolean needUpdateBuilding) {
        this.needUpdateBuilding = needUpdateBuilding;
    }

    public boolean isNeedUpdateTrain() {
        return needUpdateTrain;
    }

    public void setNeedUpdateTrain(boolean needUpdateTrain) {
        this.needUpdateTrain = needUpdateTrain;
    }

    public boolean isNeedUpdateUser() {
        if(needUpdateUser ){
            if(!ContactListBean.isCacheExist()){
                needUpdateUser = true;
                userTime = 0;
            }else {
                needUpdateUser = (userCount >0);
            }
        }

        return needUpdateUser;
    }

    public void setNeedUpdateUser(boolean needUpdateUser) {
        this.needUpdateUser = needUpdateUser;
    }

    public boolean isNeedUpdateRisk() {
        return needUpdateRisk;
    }

    public void setNeedUpdateRisk(boolean needUpdateRisk) {
        this.needUpdateRisk = needUpdateRisk;
    }

    public boolean isNeedUpdateWater() {
        return needUpdateWater;
    }

    public void setNeedUpdateWater(boolean needUpdateWater) {
        this.needUpdateWater = needUpdateWater;
    }

    public int getWaterCount() {
        return waterCount;
    }

    public void setWaterCount(int waterCount) {
        this.waterCount = waterCount;
    }

    public int getStationCount() {
        return stationCount;
    }

    public void setStationCount(int stationCount) {
        this.stationCount = stationCount;
    }

    public int getBuildingCount() {
        return buildingCount;
    }

    public void setBuildingCount(int buildingCount) {
        this.buildingCount = buildingCount;
    }

    public int getRiskCount() {
        return riskCount;
    }

    public void setRiskCount(int riskCount) {
        this.riskCount = riskCount;
    }

    public int getTrainCount() {
        return trainCount;
    }

    public void setTrainCount(int trainCount) {
        this.trainCount = trainCount;
    }

    public boolean isForceUpdate() {
        return isForceUpdate;
    }

    public void setForceUpdate(boolean forceUpdate) {
        isForceUpdate = forceUpdate;
    }

    @JSONField(serialize = false)
    public long getLastUpdateTimeByType(String map_type){
        switch (map_type){
            case GreenDaoUtils.TYPE_BUILDING:
                return buildingTime;
            case GreenDaoUtils.TYPE_RISK:
                return riskTime;
            case GreenDaoUtils.TYPE_STATION:
                return stationTime;
            case GreenDaoUtils.TYPE_TRAIN:
                return trainTime;
            case GreenDaoUtils.TYPE_UNIT:
                return unitTime;
            case GreenDaoUtils.TYPE_USER:
                return userTime;
            case GreenDaoUtils.TYPE_WATER:
                return waterTime;
        }
        return  0;
    }

    @JSONField(serialize = false)
    public void setLastUpdateTimeByType(String map_type,long time){
        switch (map_type){
            case GreenDaoUtils.TYPE_BUILDING:
                buildingTime  = time;
                break;
            case GreenDaoUtils.TYPE_RISK:
                 riskTime = time;
                 break;
            case GreenDaoUtils.TYPE_STATION:
                 stationTime = time;
                break;
            case GreenDaoUtils.TYPE_TRAIN:
                 trainTime = time;
                 break;
            case GreenDaoUtils.TYPE_UNIT:
                 unitTime = time;
                 break;
            case GreenDaoUtils.TYPE_USER:
                 userTime = time;
                 break;
            case GreenDaoUtils.TYPE_WATER:
                 waterTime = time;
                 break;
        }
    }

    @JSONField(serialize = false)
    public void setUpdateCountByType(String map_type,int  count){
        switch (map_type){
            case GreenDaoUtils.TYPE_BUILDING:
                buildingCount  = count;
                break;
            case GreenDaoUtils.TYPE_RISK:
                riskCount = count;
                break;
            case GreenDaoUtils.TYPE_STATION:
                stationCount = count;
                break;
            case GreenDaoUtils.TYPE_TRAIN:
                trainCount = count;
                break;
            case GreenDaoUtils.TYPE_UNIT:
                unitCount = count;
                break;
            case GreenDaoUtils.TYPE_USER:
                userCount = count;
                break;
            case GreenDaoUtils.TYPE_WATER:
                waterCount = count;
                break;
        }
    }

    @JSONField(serialize = false)
    public boolean isUpdateSuccess(){
        if( (waterCountUpdated == 0) && (riskCountUpdated == 0) && (trainCountUpdated  == 0) && (buildingCountUpdated  == 0) && (stationCountUpdated  == 0) && (unitCountUpdated  ==0) && (userCountUpdated  ==0)){
            return false;
        }

        return true;
    }

    @JSONField(serialize = false)
    public void sychoTimeFromOld(UserBaseInfoUpdateCache cacheOld){
        if(cacheOld == null)return;
        setUnitTime(cacheOld.getUnitTime());
        setUserTime(cacheOld.getUserTime());
        setTrainTime(cacheOld.getTrainTime());
        setBuildingTime(cacheOld.getBuildingTime());
        setRiskTime(cacheOld.getRiskTime());
        setStationTime(cacheOld.getStationTime());
        setWaterTime(cacheOld.getWaterTime());
    }

    @JSONField(serialize = false)
    public void setOneTypeNeedUpdate(String map_type){
       needUpdateUnit = false;
       needUpdateStation = false;
       needUpdateBuilding = false;
       needUpdateTrain= false;
       needUpdateUser = false;
       needUpdateRisk = false;
       needUpdateWater = false;
        switch (map_type){
            case GreenDaoUtils.TYPE_BUILDING:
                needUpdateBuilding  = true;
                break;
            case GreenDaoUtils.TYPE_RISK:
                needUpdateRisk = true;
                break;
            case GreenDaoUtils.TYPE_STATION:
                needUpdateStation = true;
                break;
            case GreenDaoUtils.TYPE_TRAIN:
                needUpdateTrain = true;
                break;
            case GreenDaoUtils.TYPE_UNIT:
                needUpdateUnit = true;
                break;
            case GreenDaoUtils.TYPE_USER:
                needUpdateUser = true;
                break;
            case GreenDaoUtils.TYPE_WATER:
                needUpdateWater = true;
                break;
        }
    }

    @JSONField(serialize = false)
    public void setAllNeedUpdate(boolean isNeed){
        needUpdateUnit = isNeed;
        needUpdateStation = isNeed;
        needUpdateBuilding = isNeed;
        needUpdateTrain= isNeed;
        needUpdateUser = isNeed;
        needUpdateRisk = isNeed;
        needUpdateWater = isNeed;
    }

    public int getUnitCountUpdated() {
        return unitCountUpdated;
    }

    public void setUnitCountUpdated(int unitCountUpdated) {
        this.unitCountUpdated = unitCountUpdated;
    }

    public int getWaterCountUpdated() {
        return waterCountUpdated;
    }

    public void setWaterCountUpdated(int waterCountUpdated) {
        this.waterCountUpdated = waterCountUpdated;
    }

    public int getStationCountUpdated() {
        return stationCountUpdated;
    }

    public void setStationCountUpdated(int stationCountUpdated) {
        this.stationCountUpdated = stationCountUpdated;
    }

    public int getUserCountUpdated() {
        return userCountUpdated;
    }

    public void setUserCountUpdated(int userCountUpdated) {
        this.userCountUpdated = userCountUpdated;
    }

    public int getBuildingCountUpdated() {
        return buildingCountUpdated;
    }

    public void setBuildingCountUpdated(int buildingCountUpdated) {
        this.buildingCountUpdated = buildingCountUpdated;
    }

    public int getRiskCountUpdated() {
        return riskCountUpdated;
    }

    public void setRiskCountUpdated(int riskCountUpdated) {
        this.riskCountUpdated = riskCountUpdated;
    }

    public int getTrainCountUpdated() {
        return trainCountUpdated;
    }

    public void setTrainCountUpdated(int trainCountUpdated) {
        this.trainCountUpdated = trainCountUpdated;
    }

    @JSONField(serialize = false)
    public void setUpdatedCountByMaptype(String map_type,int updatedCount){
        switch (map_type){
            case GreenDaoUtils.TYPE_BUILDING:
                buildingCountUpdated  = updatedCount;
                break;
            case GreenDaoUtils.TYPE_RISK:
                riskCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_STATION:
                stationCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_TRAIN:
                trainCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_UNIT:
                unitCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_USER:
                userCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_WATER:
                waterCountUpdated = updatedCount;
                break;
        }
    }

    public String  getLatestUpdateTime(){
        if( (waterTime == 0) && (riskTime == 0) && (trainTime == 0) && (buildingTime == 0) && (stationTime == 0) && (unitTime ==0) && (userTime ==0)){
            return "";
        }
        ArrayList<Long> list = new ArrayList<>();
        list.add(waterTime);
        list.add(riskTime);
        list.add(trainTime);
        list.add(buildingTime);
        list.add(stationTime);
        list.add(unitTime);
        list.add(userTime);

        for(int i = 0 , l = list.size(); i < l; i ++){
            for(int j=0;j<list.size()-i-1;j++){//比较两个整数
                if(list.get(j)>list.get(j+1)){
                    /*交换*/
                    long temp=list.get(j);
                    list.set(j, list.get(j+1));
                    list.set(j+1, temp);
                }
            }
        }
//        for(long i:list)//输出结果
//           LogUtil.i("--->"+i);

        return  TimeUtils.getTimeLongToStrByFormat(list.get(list.size()-1),"yyyy-MM-dd HH:mm");
    }
}
